export const getSessionData = (key: string): string | null => sessionStorage.getItem(key);
export const setSessionData = (key: string, value: any): void => {
  if (value == null) {
    sessionStorage.removeItem(key);
    return;
  }
  sessionStorage.setItem(key, value.toString());
};

export function getSessionJson<T = any>(key: string): T | null;
export function getSessionJson<T = any>(key: string, defVal: T): T;
export function getSessionJson<T = any>(key: string, defVal?: T): T | null {
  const value = sessionStorage.getItem(key);
  if (value != null) {
    return JSON.parse(value);
  }
  return defVal != null ? defVal : null;
}

export const setSessionJson = <T>(key: string, value: T): void => {
  if (value == null) {
    sessionStorage.removeItem(key);
    return;
  }
  sessionStorage.setItem(key, JSON.stringify(value));
};

export const removeSessionData = (key: string): void => sessionStorage.removeItem(key);

export const getLocalData = (key: string): string | null => localStorage.getItem(key);
export const setLocalData = (key: string, value: any): void => {
  if (value == null) {
    localStorage.removeItem(key);
    return;
  }
  localStorage.setItem(key, value.toString());
};

export function getLocalJson<T = any>(key: string): T | null;
export function getLocalJson<T = any>(key: string, defVal: T): T;
export function getLocalJson<T = any>(key: string, defVal?: T): T | null {
  const value = localStorage.getItem(key);
  if (value != null) {
    return JSON.parse(value);
  }
  return defVal != null ? defVal : null;
}

export const setLocalJson = <T>(key: string, value: T): void => {
  if (value == null) {
    localStorage.removeItem(key);
    return;
  }
  localStorage.setItem(key, JSON.stringify(value));
};

export const removeLocalData = (key: string): void => localStorage.removeItem(key);
